package com.skynet.whwpro.module;

import com.skynet.whwpro.bean.BeanDyb;
import com.skynet.whwpro.bean.BeanSjzcb;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.*;

import java.util.List;

@At("/yhbzxcx_dyb")
@IocBean
public class DybModule {

    @Inject
    private Dao dao;

    //统计数量
    @Ok("raw")
    @At
    public long count() {
        return dao.count(BeanDyb.class);
    }

    //根据字段查询
    @At
    public NutMap query(@Param("userid") String userid) {
        List<BeanDyb> users = dao.query(BeanDyb.class, Cnd.where("dyuserid","=",userid).orderBy().desc("dyuserid"));
        return new NutMap("ok", true).setv("data", users);
    }

    //增加数据
    @At
    @POST
    public NutMap add(@Param("..") BeanDyb BeanDyb) {
        if (Strings.isBlank(BeanDyb.getDyuserid()))
            return new NutMap("ok", false);
        dao.insert(BeanDyb);
        return new NutMap("ok", true);
    }

    //根据id删除数据
    @At
    @POST
    public NutMap delete(long id) {
        dao.clear(BeanDyb.class, Cnd.where("dyuserid", "=", id));
        return new NutMap("ok", true);
    }

    //更新数据
    @At
    @POST
    @AdaptBy(type= JsonAdaptor.class)
    public NutMap update(@Param("..") BeanDyb user) {
        dao.update(user);
        return new NutMap("ok", true);
    }
}
